package com.example.homework.baseActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.homework.R;
import com.example.homework.db.DBHelper;

public class AddActivity extends Activity {
    // 声明组件
    private EditText nameEditText;
    private EditText ageEditText;
    private EditText sexEditText;
    private EditText eduEditText;
    private Button addButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        nameEditText = findViewById(R.id.name);
        ageEditText = findViewById(R.id.age);
        sexEditText = findViewById(R.id.sex);
        eduEditText = findViewById(R.id.edu);
        addButton = findViewById(R.id.ButtonAdd);

        // 根据ID 获取组件并设置添加按钮的点击监听器
        addButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取各个EditText中的文本内容，并转换为字符串类型
                String name = nameEditText.getText().toString();
                String age = ageEditText.getText().toString();
                String sex = sexEditText.getText().toString();
                String edu = eduEditText.getText().toString();

                // 封装信息
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("age", age);
                values.put("sex", sex);
                values.put("edu", edu);

                DBHelper helper = new DBHelper(getApplicationContext());
                helper.insert(values);

                Intent intent = new Intent(AddActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}